package com.ray.user;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.ray.base.adapter.CommonRecyclerAdapter;
import com.ray.base.adapter.RecycleViewDivider;
import com.ray.base.adapter.ViewHolder;
import com.ray.base.base.BaseFragment;
import com.ray.base.bean.FunctionBean;
import com.ray.base.router.RouterActivityPath;
import com.ray.base.router.RouterFragmentPath;
import com.ray.base.utils.ImageUtils;
import com.ray.base.utils.ToastUtils;
import com.ray.base.viewmodel.NoViewModel;
import com.ray.user.databinding.FragmentUserBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;


/**
 * 用户界面(我的)
 **/
@Route(path = RouterFragmentPath.User.PAGER_ME)
public class UserFragment extends BaseFragment<FragmentUserBinding, NoViewModel> implements View.OnClickListener {

    @Autowired
    Boolean IS = false;
    String phonenumber;
    private List<FunctionBean> mLists = new ArrayList<>();
    private CommonRecyclerAdapter<FunctionBean> adapter;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_user;
    }

    @Override
    public void loadData() {
        Bundle bundle = getActivity().getIntent().getExtras();
        phonenumber = bundle.getString("number");
        if (phonenumber != null) {
            ImageUtils.newInstance().displayCircle("https://gitee.com/lucap/frozen/raw/master/screenshot/icon_fengbaobao.jpg", binding.ivHead);
            binding.tvName.setVisibility(View.VISIBLE);
            binding.tvName.setText("用户:" + phonenumber);
            binding.ivHead.setVisibility(View.VISIBLE);
            binding.login.setVisibility(View.GONE);
        } else {
            binding.login.setText("点击登陆");
            binding.login.setOnClickListener(this);
        }


        mLists.clear();
        mLists.add(new FunctionBean("01", "设置", R.drawable.ic_settings_24dp));
        mLists.add(new FunctionBean("02", "安全", R.drawable.ic_fingerprint_24dp));
        mLists.add(new FunctionBean("03", "收藏", R.drawable.ic_star_border_black_24dp));
        mLists.add(new FunctionBean("04", "缓存", R.drawable.ic_cloud_queue_black_24dp));
        mLists.add(new FunctionBean("05", "记录", R.drawable.ic_live_tv_black_24dp));
        mLists.add(new FunctionBean("06", "评价", R.drawable.ic_sentiment_very_satisfied_24dp));

        initAdapter();
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new CommonRecyclerAdapter<FunctionBean>(getContext(), mLists, R.layout.function_item) {
                @Override
                public void convert(ViewHolder holder, final FunctionBean item, int position) {
                    holder.setImageResource(R.id.iv_resource, item.getResourceId());
                    holder.setText(R.id.tv_name, item.getName());
                    holder.setOnIntemClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View view) {
                            switch (item.getName()) {
                                case "设置":
                                    ARouter.getInstance()
                                            .build(RouterActivityPath.EyeVideo.PAGER_DownloadSet)
                                            .navigation();
                                    break;
                                case "缓存":
                                    if (phonenumber == null) {
                                        ToastUtils.showShort("您还没有登陆,请登录");
                                        //跳转到登陆界面
                                        sendCode(getActivity());
                                    } else {
                                        ARouter.getInstance()
                                                .build(RouterActivityPath.EyeVideo.PAGER_Download)
                                                .navigation();
                                    }
                                    break;
                                case "记录":
                                    if (phonenumber == null) {
                                        ToastUtils.showShort("您还没有登陆,请登录");
                                        //跳转到登陆界面
                                        sendCode(getActivity());
                                    } else {
                                        ARouter.getInstance()
                                                .build(RouterActivityPath.EyeVideo.PAGER_RECORD)
                                                .navigation();
                                    }
                                    break;
                                case "收藏":
                                    if (phonenumber == null) {
                                        ToastUtils.showShort("您还没有登陆,请登录");
                                        //跳转到登陆界面
                                        sendCode(getActivity());
                                    } else {
                                        ARouter.getInstance()
                                                .build(RouterActivityPath.EyeVideo.PAGER_FAV)
                                                .navigation();
                                    }
                                    break;
                                case "评价":
                                    ToastUtils.showLong("请把你宝贵的意见发送到1255676712@qq.com!");
                                    break;
                                default:
                                    ToastUtils.showShort(item.getName());
                                    break;
                            }
                        }
                    });
                }
            };
        }
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerView.setAdapter(adapter);
        //设置分割线 方向 颜色
        binding.recyclerView.addItemDecoration(new RecycleViewDivider(getContext(), LinearLayoutManager.VERTICAL, 1, getResources().getColor(R.color.bg_f4)));
    }

    //mobtech默认短信验证方法
    public void sendCode(Context context) {
        RegisterPage page = new RegisterPage();
        //如果使用我们的ui，没有申请模板编号的情况下需传null
        page.setTempCode(null);
        page.setRegisterCallback(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    // 处理成功的结果
                    HashMap<String, Object> phoneMap = (HashMap<String, Object>) data;
                    // 国家代码，如“86”
                    String country = (String) phoneMap.get("country");
                    // 手机号码，如“13800138000”
                    String phone = (String) phoneMap.get("phone");
                    phonenumber = phone;
                    System.out.println(phonenumber);
                    ARouter.getInstance()
                            .build(RouterActivityPath.Main.PAGER_MAIN)
                            .withString("number", phonenumber)
                            .navigation();
                } else {
                    // TODO 处理错误的结果
                }
            }
        });
        page.show(context);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.login)
            sendCode(getActivity());
    }
}
